<?php

$memDebugStart = memory_get_usage();

$strSpendTime = "start:".date("Y-m-d H:i:s") . "\n";

header("Content-type: text/html; charset=UTF-8");
include(dirname(__FILE__) . "/global.php");

include(INCLUDE_PATH."/common/common.func.php");

$timeStart = get_microtime();

if(!isset($_POST['action']) || !isset($_POST['command'])) {
	exit('Access Denied');
}

require(INCLUDE_PATH . "/common/language_file.class.php");
require(INCLUDE_PATH . "/common/session.class.php");
require(INCLUDE_PATH . "/common/php_redis.class.php");
require(INCLUDE_PATH . "/common/manage_redis.class.php");
require(INCLUDE_PATH . "/online/online.class.php");
require(INCLUDE_PATH . "/mode/online_mode.class.php");

$action		= $_POST['action'];
$command	= $_POST["command"];

$log = "start:".date("Y-m-d H:i:s").":\n";
foreach($_POST as $key => $value) {
	$log .= $key . ":" . $value."\n";
}
$log .= "end";
wlog($action,$log);

#数据库连接

/*
$objManageRedis = new manage_redis();

$objManageRedis->set_rb("DB_W","rw",$RedisConfig['master'],SERVER_ID);	//读或写
$objManageRedis->set_rb("DB_R","r",$RedisConfig['slave'],SERVER_ID);		//只读
*/

$objRd			= new php_redis(RD_MASTER_HOST,RD_MASTER_PORT,RD_DB_NAME);
$objRd->bRdDebug = true;

$objOnline	= new online();
$objOnline->set_rd(& $objRd);

$objSession = new Session($objRd,$objOnline);

include(PROJECT_ROOT . "/auth.php");								//登录验证

$timeStop = get_microtime();
$strSpendTime .= "use time a:" . ($timeStop - $timeStart) . "\n";
$hTimeStart = get_microtime();

$GLOBALS['arrAuth'] = $arrAuth;

$time = time();


require_once(INCLUDE_PATH . "/do/do_action.class.php");

$objDoAction = new do_action();
$output = $objDoAction->deal($action,$command,$_POST);

print_r($output);

session_write_close();

if($arrAuth["p_id"] == 1008)
	wlog("newer",$action . ":" . $command . ":" . $arrAuth["p_id"].":".$arrAuth["newer"]);

$objRd->close();

unset($objDoAction,$objSession);

if(defined('DEBUG') && DEBUG == true) {
	$strTWarningDebug = "";
	$strMWarningDebug = "";
	/*--------------------------------------------[ mem debug ]--------------------------------- [ begin ]---*/
	$memDebugStop = memory_get_usage();
	$memDebugUse = ($memDebugStop - $memDebugStart) / 1024;
	$strMemLogPath = LOG_PATH . "/memdebug/";
	if(!is_dir($strMemLogPath)){
		mkdir($strMemLogPath);
	}

	$fp = fopen($strMemLogPath.$action."-".$command,"a");
	fwrite($fp,"begin:==============================================================\n");
	fwrite($fp,$arrAuth["p_id"] . ":" . ($memDebugStart/1024) . ":" . $memDebugUse . "kb\n");
	fwrite($fp,"================================================================:end\n");
	fclose($fp);

	if($memDebugUse > 1000) {
		$strMWarningDebug .= "memDebug begin:===================================================================\n";
		$strMWarningDebug .= $memDebugUse . "kb\n";
		$strMWarningDebug .= "================================================================memDebug :end\n";
	}
	/*--------------------------------------------[ mem debug ]--------------------------------- [ end ]---*/


	/*--------------------------------------------[ rd debug ]--------------------------------- [ begin ]---*/
	$strRdLogPath = LOG_PATH . "/rddebug/";
	if(!is_dir($strRdLogPath)){
		mkdir($strRdLogPath);
	}
	$fp = fopen($strRdLogPath.$action."-".$command,"a");
	fwrite($fp,"begin:===============================================================\n");
	fwrite($fp,$objRd->rdDebug);
	fwrite($fp,"=================================================================:end\n");
	fclose($fp);
	/*--------------------------------------------[ rd debug ]--------------------------------- [ end ]---*/


	/*--------------------------------------------[ time debug ]--------------------------------- [ begin ]---*/

	$timeStop = get_microtime();
	$strTimeLogPath = LOG_PATH . "/timedebug/";
	if(!is_dir($strTimeLogPath)){
		mkdir($strTimeLogPath);
	}
	$timeSpend = $timeStop - $timeStart;
	$fp = fopen($strTimeLogPath.$action."-".$command,"a");
	fwrite($fp,"begin:===============================================================\n");
	fwrite($fp,"use " . ($timeSpend) . "  sec \n");
	fwrite($fp,$arrAuth["p_id"] .":".$strSpendTime . "end".date("Y-m-d H:i:s")."\n");
	fwrite($fp,"=================================================================:end\n");
	fclose($fp);

	if($timeSpend > 0.3) {
		$strTWarningDebug .= "timeDebug begin:================================================================\n";
		$strTWarningDebug .= "use " . ($timeSpend) . "  sec \n";
		$strTWarningDebug .= "================================================================timeDebug :end\n";
	}
	/*--------------------------------------------[ time debug ]--------------------------------- [ end ]---*/

	/*--------------------------------------------[ warning debug ]--------------------------------- [ begin ]---*/
	if($strTWarningDebug != "") {
		$strWarningLogPath = LOG_PATH . "/twarningdebug/";
		if(!is_dir($strWarningLogPath)){
			mkdir($strWarningLogPath);
		}
		$fp = fopen($strWarningLogPath.$action."-".$command,"a");
		fwrite($fp,"begin:===============================================================\n");
		fwrite($fp,$strTWarningDebug ." \n");
		fwrite($fp,"=================================================================:end\n");
		fclose($fp);
	}
	if($strMWarningDebug != "") {
		$strWarningLogPath = LOG_PATH . "/mwarningdebug/";
		if(!is_dir($strWarningLogPath)){
			mkdir($strWarningLogPath);
		}
		$fp = fopen($strWarningLogPath.$action."-".$command,"a");
		fwrite($fp,"begin:===============================================================\n");
		fwrite($fp,$strMWarningDebug ." \n");
		fwrite($fp,"=================================================================:end\n");
		fclose($fp);
	}
	/*--------------------------------------------[ warning debug ]--------------------------------- [ end ]---*/
}

unset($objRd);

?>